﻿using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;

namespace CartoLab.GeometryLib.AlgorithmExtent
{

    /// <summary>
    /// 用于多制图比例尺
    /// </summary>
    public class CartoScaleInfo
    {
        public CartoScaleInfo() { }
        public CartoScaleInfo(bool selected, double scale, string path)
        {
            _selected = selected;
            _cartoScale = scale;
            _libPath = path;　
        }

        private bool _selected = true;
        /// <summary>
        /// 存储当前文档中是否选中该比例尺
        /// </summary>
        public bool Selected
        {
            get { return _selected; }
            set { _selected = value; }
        }

        private double _cartoScale = 0;
        /// <summary>
        /// 制图比例尺
        /// </summary>
        public double CartoScale
        {
            get { return _cartoScale; }
            set { _cartoScale = value; }
        }
        private string _libPath = "";

        /// <summary>
        /// 符号库文件名，其路径为应用程序目录下的symbolDB中
        /// </summary>
        public string SymbolDBFileName 
        {
            get { return _libPath; }
            set { _libPath = value; }
        }
    }

    public class Algorithm
    {
        #region xml配置文档的各个元素名称
        private const string ROOT_NAME = "AppConfigurationInfo";
        private const string CARTO_TRANSFORM_INFO = "制图转换字段";
        private const string MAP_DOCUMENT_SCALE = "文档比例尺";
        private const string CARTO_SCALES_NODE = "地图比例尺";
        private const string EACH_CARTO_SCALE_NODE = "单个地图比例尺";
        private const string SELECTED_CARTO_SCALE = "是否选中";
        private const string CARTO_SCALE = "比例尺";
        private const string SYMBOL_DB_FILE_NAME = "符号库";
        #endregion

        //#region 初始化配置信息xml，包含制图转换字段
        ///// <summary>
        ///// 初始化配置信息xml，包含制图转换字段
        ///// modified by fish 2011.05.11 13:20
        ///// </summary>
        //public static void InitialConfigInfo(string path)
        //{
        //    //看配置文件存不存在，不存在创建
        //    CreateConfigurationInfoFile(path);
        //}
        //#endregion

        #region 创建配置文件信息
        /// <summary>
        /// 创建配置文件信息
        /// </summary>
        /// <param name="path"></param>
        private static void CreateConfigurationInfoFile(string path)
        {
            if (!File.Exists(path))
            {
                FileStream fs = new FileStream(path, FileMode.OpenOrCreate);
                //写入配置信息
                XmlTextWriter writer = new XmlTextWriter(fs, Encoding.UTF8);
                writer.WriteStartDocument();

                writer.WriteStartElement(ROOT_NAME);
                ////writer.WriteElementString("制图转换字段", "GB");
                //writer.WriteElementString(DeployElementName.CARTO_TRANSFORM_FIELD_NAME, "GB");
                //writer.WriteElementString("符号库", "");
                //writer.WriteElementString("比例尺", "");
                //writer.WriteEndElement();

                writer.WriteEndDocument();
                writer.Close();
            }
        }
        #endregion

        //#region 读取比例尺配置文件信息
        ///// <summary>
        ///// 读取比例尺配置文件信息
        ///// </summary>
        ///// <param name="path"></param>
        //public static string ReadConfigurationScale(string path)
        //{
        //    XmlDocument xml = new XmlDocument();
        //    if (File.Exists(path))
        //    {
        //        xml.Load(path);
        //        //取得所有的setting
        //        XmlNode xn = xml.SelectSingleNode("AppConfigurationInfo/比例尺");
        //        if (xn != null)
        //        {
        //            return xn.InnerText;
        //        }   
        //    }
        //    return "";
        //}
        //#endregion

        //#region 读取制图转换字段配置文件信息
        ///// <summary>
        ///// 读取制图转换字段配置文件信息
        ///// </summary>
        ///// <param name="path"></param>
        //public static string ReadConfigurationInfoFileCartoMappingFiled(string path)
        //{
        //    XmlDocument xml = new XmlDocument();
        //    if (File.Exists(path))
        //    {
        //        xml.Load(path);
        //        //取得所有的setting
        //        XmlNodeList xn = xml.GetElementsByTagName("AppConfigurationInfo");
        //        //遍历所有的setting
        //        foreach (XmlElement xe in xn)
        //        {
        //            foreach (XmlNode node in xe.ChildNodes)
        //            {
        //                if (node.Name == "制图转换字段")
        //                {
        //                    return node.InnerText;
        //                }
        //            }
        //        }
        //    }
        //    return "";
        //}
        //#endregion

        //#region 读取配置文件信息
        ///// <summary>
        ///// 读取符号库配置文件信息
        ///// </summary>
        ///// <param name="path"></param>
        //public static string ReadConfigurationInfoFileSymbolDataBase(string path)
        //{
        //    XmlDocument xml = new XmlDocument();
        //    if (File.Exists(path))
        //    {
        //        xml.Load(path);
        //        //取得所有的setting
        //        XmlNodeList xn = xml.GetElementsByTagName("AppConfigurationInfo");
        //        //遍历所有的setting
        //        foreach (XmlElement xe in xn)
        //        {
        //            foreach (XmlNode node in xe.ChildNodes)
        //            {
        //                if (node.Name == "符号库")
        //                {
        //                    if (node.InnerText != "")
        //                    {
        //                        return node.InnerText;
        //                    }
        //                }
        //            }
        //        }
        //    }
        //    return "";
        //}
        //#endregion

        #region 读取比例尺配置文件信息
        /// <summary>
        /// 读取比例尺配置文件信息
        /// </summary>
        /// <param name="path"></param>
        public static string ReadMapDocScaleFromConfiguration(string path)
        {
            XmlDocument xml = new XmlDocument();
            if (File.Exists(path))
            {
                try
                {
                    xml.Load(path);
                    //取得所有的setting
                    string strElementName = ROOT_NAME + "/" + MAP_DOCUMENT_SCALE;
                    XmlNode xn = xml.SelectSingleNode(strElementName);
                    if (xn != null)
                    {
                        return xn.InnerText;
                    }
                }
                catch
                {
                    return "";
                }
            }
            return "";
        }
        #endregion

        #region 读取制图转换字段配置文件信息
        /// <summary>
        /// 读取制图转换字段配置文件信息
        /// </summary>
        /// <param name="path"></param>
        public static string ReadCartoMappingInfoFromConfiguration(string path)
        {
            XmlDocument xml = new XmlDocument();
            if (File.Exists(path))
            {
                try
                {
                    xml.Load(path);
                    //取得所有的setting
                    string strElementName = ROOT_NAME + "/" + CARTO_TRANSFORM_INFO;
                    XmlNode xn = xml.SelectSingleNode(strElementName);
                    if (xn != null)
                    {
                        return xn.InnerText;
                    }
                }
                catch
                {
                    return "";
                }
            }
            return "";
        }
        #endregion

        #region 读取多制图比例尺信息
        /// <summary>
        /// 读取多制图比例尺信息
        /// </summary>
        /// <param name="path"></param>
        public static List<CartoScaleInfo> ReadMulitiCartoScaleInfoFromConfiguration(string path)
        {
            List<CartoScaleInfo> cartoScaleInfoList = new List<CartoScaleInfo>();
            XmlDocument xml = new XmlDocument();
            CartoScaleInfo csInfo = null;
            if (File.Exists(path))
            {
                try
                {
                    xml.Load(path);
                    //取得所有的setting
                    string strElementName = ROOT_NAME + "/" + CARTO_SCALES_NODE + "/" + EACH_CARTO_SCALE_NODE;
                    System.Collections.IEnumerator  xnEnum = xml.SelectNodes(strElementName).GetEnumerator();
                    while (xnEnum.MoveNext())
                    {
                        XmlNode eachNode = xnEnum.Current as XmlNode;
                        if (eachNode != null)
                        {
                            csInfo = new CartoScaleInfo();

                            XmlNode xn = eachNode.SelectSingleNode(SELECTED_CARTO_SCALE);
                            bool seled = true;
                            if (xn != null)
                            {
                                if (bool.TryParse(xn.InnerText, out seled))
                                {
                                    csInfo.Selected = seled;
                                }
                            }

                            xn = eachNode.SelectSingleNode(CARTO_SCALE);
                            int scale = 0;
                            if (xn != null)
                            {
                                if (int.TryParse(xn.InnerText, out scale))
                                {
                                    csInfo.CartoScale = scale;
                                }
                            }

                            xn = eachNode.SelectSingleNode(SYMBOL_DB_FILE_NAME);
                            csInfo.SymbolDBFileName = xn.InnerText;
                            cartoScaleInfoList.Add(csInfo);
                        }
                    }
                    return cartoScaleInfoList;
                }
                catch
                {
                    return null;
                }
            }
            return null;
        }
        #endregion

        #region 存储地图文档信息到配置文件中，对外方法
        public static bool SaveAllDocInfoIntoConfiguration(string strClassfiFieldValue, 
            string mapDocScale, List<CartoScaleInfo> cartoScaleList,string path, out string errorMsg)
        {
            if (!File.Exists(path))
            {
                //如果配置文件不存在，则新建一个
                try
                {
                    CreateConfigurationInfoFile(path);
                }
                catch (System.Exception ex)
                {
                    errorMsg = ex.Message;
                    return false;
                }
            }
            XmlDocument doc = new XmlDocument();
                doc.Load(path);
            XmlNode root = doc.DocumentElement;
            root.RemoveAll();

            //存储顺序不能变
            //1. 先存储制图转换字段
            SaveClassifyField(doc, root, strClassfiFieldValue, path);

            //2. 存储文档比例尺
            SaveMapDocScale(doc, root, mapDocScale, path);

            //3. 存储地图比例尺
            SaveCartoScales(doc, root, cartoScaleList, path);

            doc.Save(path);

            errorMsg = "存储成功";
            return true;
        }
        #endregion


        #region 将制图转换字段存入到配置信息文件
        /// <summary>
        /// 将制图转换字段存入到配置信息文件
        /// </summary>
        /// <param name="CartoMappingField"></param>
        private static void SaveClassifyField(XmlDocument doc, XmlNode node, string value, string path)
        {
            //XmlNode root = doc.DocumentElement;
            XmlElement elem = doc.CreateElement(CARTO_TRANSFORM_INFO);
            elem.InnerText = value;
            node.AppendChild(elem);
        }
        #endregion

        #region 将比例尺字段存入到配置信息文件，包括文档比例尺和制图比例尺
        /// <summary>
        /// 将文档比例尺字段存入到配置信息文件
        /// </summary>
        /// <param name="CartoMappingField"></param>
        private static void SaveMapDocScale(XmlDocument doc, XmlNode node, string value, string path)
        {
            /*
             * 此部分文档结果如下所示：
             *   <文档比例尺>50000</文档比例尺>  
             **/
            XmlElement elem = doc.CreateElement(MAP_DOCUMENT_SCALE);
            elem.InnerText = value;
            node.AppendChild(elem);

            //XmlDocument doc = new XmlDocument();
            //doc.Load(path);
            //XmlNode node = doc.DocumentElement;
            //XmlNode age = root.SelectSingleNode("比例尺");
            //XmlNode age = root.SelectSingleNode(DeployElementName.MAP_DOCUMENT_SCALE);
            //if (age != null)
            //{
            //    age.InnerText = value;
            //}
            //doc.Save(path);
        }

        private static void SaveCartoScales(XmlDocument doc, XmlNode node, List<CartoScaleInfo> cartoScaleList, string path)
        {
            /*
             * 此部分文档结果如下所示：
             *   <地图比例尺>
	         *      <单个地图比例尺>
	  	     *         <比例尺>250000</比例尺>
	  	     *         <符号库>1比25万符号库.xml</符号库>
	         *      </单个地图比例尺>
	         *      <单个地图比例尺>
	  	     *         <比例尺>50000</比例尺>
	  	     *         <符号库>1比5万符号库_DM版本.xml</符号库>
	         *      </单个地图比例尺>
             *   </地图比例尺>
             **/
            //XmlDocument doc = new XmlDocument();
            //doc.Load(path);
            //XmlNode root = doc.DocumentElement;
            XmlNode cartoScaleNode = doc.CreateElement(CARTO_SCALES_NODE);            
            foreach (CartoScaleInfo csi in cartoScaleList.ToArray())
            {
                XmlNode eachNode = doc.CreateElement(EACH_CARTO_SCALE_NODE);

                XmlElement elem = doc.CreateElement(SELECTED_CARTO_SCALE);
                elem.InnerText = csi.Selected.ToString();
                eachNode.AppendChild(elem);

                elem = doc.CreateElement(CARTO_SCALE);
                elem.InnerText = csi.CartoScale.ToString();
                eachNode.AppendChild(elem);

                elem = doc.CreateElement(SYMBOL_DB_FILE_NAME);
                elem.InnerText = csi.SymbolDBFileName.ToString();
                eachNode.AppendChild(elem);

                cartoScaleNode.AppendChild(eachNode);
            }
            node.AppendChild(cartoScaleNode);
            //doc.Save(path);
        }
        #endregion

        //#region 将符号库存入到配置信息文件
        ///// <summary>
        ///// 将符号库存入到配置信息文件
        ///// </summary>
        ///// <param name="value"></param>
        //public static void SaveSymbolDatabaseConfigurationInfo(string value, string path)
        //{
        //    XmlDocument doc = new XmlDocument();
        //    doc.Load(path);
        //    XmlNode root = doc.DocumentElement;
        //    XmlNode age = root.SelectSingleNode("符号库");
        //    if (age != null)
        //    {
        //        age.InnerText = value;
        //        doc.Save(path);
        //    }
        //}
        //public static void SaveSymbolDatabaseConfigurationInfo(List<CartoScaleInfo> CartoScaleList, string path)
        //{
        //    XmlDocument doc = new XmlDocument();
        //    doc.Load(path);
        //    XmlNode root = doc.DocumentElement;
        //    foreach (CartoScaleInfo csi in CartoScaleList.ToArray())
        //    {
        //        XmlElement elem = doc.CreateElement("符号库");
        //        elem.InnerText = csi.SymbolDBFileName;
        //        root.AppendChild(elem);
        //    }
        //    doc.Save(path);
        //}
        //#endregion
    }
}
